﻿class BooktablesController < ApplicationController
  # GET /booktables
  # GET /booktables.json
  def index
    #@booktable = Booktable.all

    @booktables = Booktable.search(params[:search])
    
    
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @booktables }
    end
  end

  # GET /booktables/1
  # GET /booktables/1.json
  def show
    @booktable = Booktable.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @booktable }
    end
  end

  # GET /booktables/new
  # GET /booktables/new.json
  def new
    @booktable = Booktable.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @booktable }
    end
  end

  # GET /booktables/1/edit
  def edit
    @booktable = Booktable.find(params[:id])
  end

  # POST /booktables
  # POST /booktables.json
  def create
    @booktable = Booktable.new(params[:booktable])

    respond_to do |format|
      if @booktable.save
        format.html { redirect_to @booktable, notice: 'Thêm sách thành công' }
        format.json { render json: @booktable, status: :created, location: @booktable }
      else
        format.html { render action: "new" }
        format.json { render json: @booktable.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /booktables/1
  # PUT /booktables/1.json
  def update
    @booktable = Booktable.find(params[:id])

    respond_to do |format|
      if @booktable.update_attributes(params[:booktable])
        format.html { redirect_to @booktable, notice: 'Cập nhật sách thành công' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @booktable.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /booktables/1
  # DELETE /booktables/1.json
  def destroy
    @booktable = Booktable.find(params[:id])
    @booktable.destroy

    respond_to do |format|
      format.html { redirect_to booktables_url }
      format.json { head :no_content }
    end
  end
end
